var order_room_id = null
var myInvitedUserNotice = null
var detailJsFunction = null
var co = require('common.js')
function initWebsocket(userInfo, roomId, callback) {
   wx.connectSocket({
      url: 'wss://wss.suiyuantech.com',
      success: function (res) {
         console.log('【wx.connectSocket连接成功】')
         wx.onSocketOpen(function (res) {
            console.log('【wx.onSocketOpen打开成功】')
            loginWebsocket(userInfo, roomId, callback)
            if (order_room_id) {
               loginWebsocket(userInfo, order_room_id, callback)
            }
         })
      },
      fail: function (res) {
         console.log('wx.connectSocket连接失败')
      },
   })

   wx.onSocketClose(function (res) {
      console.log('【【wx.onSocketClose关闭了】】', res)
      // wx.showModal({
      //    title: 'Socket关闭了',
      //    content: 'order_room_id=' + order_room_id,
      // })
      initWebsocket(userInfo, roomId, callback)
   })
}

function loginWebsocket(userInfo, roomId, callback) {
   if (roomId != 29354) {
      order_room_id = roomId
   }
   var json = {
      type: userInfo.type,
      unionid: userInfo.unionid,
      client_name: userInfo.nickName,
      room_id: roomId//卷帘门维修专用room_id:29354
   }
   if (userInfo.type == 'logout') {
      json.client_id = userInfo.client_id
   } else {
      json.avatarUrl = userInfo.avatarUrl
      json.inviter_uid = userInfo.inviter_uid
   }
   var msg = JSON.stringify(json)
   sendSocketMsg(msg, callback)
}

function sendSocketMsg(msg, callback) {
   wx.sendSocketMessage({
      data: msg,
      success: function (res) {
         console.log('【sendSocketMsg:发送成功】typeof (callback)==', typeof callback)
         if (typeof (callback) == 'function') {
            callback(res)
         }
      },
      fail: function (res) {
         console.log('sendSocketMsg:发送失败')
      },
   })
}

function sendToAll(content, callback) {
   var msg = {
      type: 'say',
      to_client_id: 'all',
      content: content
   }
   var msgJson = JSON.stringify(msg)
   sendSocketMsg(msgJson, callback)
}

function sendTo() {

}

function onSocketMsg(callback) {
   wx.onSocketMessage(function (res) {
      var obj = JSON.parse(res.data)
      if (obj.type != 'ping') {
         callback(obj)
      }

      if (obj.type != 'ping' && detailJsFunction && typeof (detailJsFunction) == 'function') {
         detailJsFunction(obj)
      }

      // if (obj.type == 'login') {
      //    wx.showModal({
      //       title: '登入提示',
      //       content: obj.client_id + '==' + obj.client_name + ' 登入' + obj.room_id
      //    })
      // } else if (obj.type == 'logout') {
      //    wx.showModal({
      //       title: '退出提示',
      //       content: obj.from_client_id + '==' + obj.from_client_name + ' 退出' + obj.from_room_id
      //    })
      // } else 
      if (obj.type == 'reg' && myInvitedUserNotice && typeof (myInvitedUserNotice) == 'function') {
         myInvitedUserNotice(obj)
      }
   })
}
//设置detail.js页面的监听回调
function setDetailJsOnSocket(detail_js_function) {
   if (typeof (detail_js_function) == 'function') {
      detailJsFunction = detail_js_function
      // co.toast(detail_js_function + '监听设置成功')
   } else {
      co.toast(detail_js_function + '不是function，设置监听失败')
   }
}

//设置我邀请的人的监听
function setNoticeAddMyInvitedUser(notice) {
   myInvitedUserNotice = notice
}

function setDataOrderRoomId(roomId) {
   order_room_id = roomId
   // wx.showToast({ title: 'rid=' + order_room_id })
   console.log('【order_room_id】', order_room_id)
}

module.exports = {
   initWebsocket: initWebsocket,
   onSocketMsg: onSocketMsg,
   loginWebsocket: loginWebsocket,
   sendSocketMsg, sendSocketMsg,
   sendToAll: sendToAll,
   sendTo: sendTo,
   setNoticeAddMyInvitedUser: setNoticeAddMyInvitedUser,
   setDataOrderRoomId: setDataOrderRoomId,
   setDetailJsOnSocket: setDetailJsOnSocket
}

